#include "wsocket.h"
#include <stdarg.h>

int errexit( const char* format,...){
	va_list args;
	va_start(args, format);
	vfprintf(stderr, format, args);
	va_end(args);
	exit(1);
	return 0;
}
int passiveTCP( const char* service, int qlen){
	passivesock( service, "TCP", qlen);
}
int passiveUDP( const char* service, int qlen){
	passivesock( service, "UDP", qlen);
}
int passivesock( const char* service, const char *transport, int qlen){
	struct servent *pse;
	struct protoent *ppe;
	struct sockaddr_in sin;
	int s, type;
	memset(&sin, 0, sizeof(sin) );
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	/*
	if( pse = getservbyname( service, transport ) ){
		sin.sin_port = htons( ntohs( (u_short)pse->s_port ) + 0 );
		printf("%d \n", sin.sin_port);
	}
	else if( (sin.sin_port = htons( (u_short)atoi(service))) == 0)
		errexit( "can't get \"%s\" service entry \n", service);
		*/
	sin.sin_port = htons( 4321 );
	if( (ppe = getprotobyname(transport)) ==0 )
		errexit("can't get \"%s\" protocol entry \n", transport);
	if( strcmp( transport, "udp" ) == 0 )
		type = SOCK_DGRAM;
	else
		type = SOCK_STREAM;
	s = socket( AF_INET, type , ppe->p_proto );
	if( s < 0 ){
		errexit("can't create socket:%s \n", strerror(errno));
	}
	if( bind( s, (struct sockaddr* )&sin, sizeof( sin ) )< 0 ){
		errexit("can't bind to %s port: %s \n", service, strerror(errno) );
	}
	if( type == SOCK_STREAM && listen( s, qlen) < 0 )
		errexit( "can't listen on %s port: %s \n", service, strerror(errno));
	return s;
}

int connectTCP( const char* host, const char* service){
	return connectsock( host, service, "tcp");
}

int connectUDP( const char* host, const char* service){
	return connectsock( host,service, "udp");
}

int connectsock( const char* host, const char* service, const char* transport){
	struct hostent *phe;//pointer to host information entry 
	struct servent *pse;//pointer to service information entry
	struct protoent *ppe;//pointer to protocol information entry
	struct sockaddr_in sin;
	int s, type;//socket descripter and socket type
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	/*
	//Map service name to port number
	if( pse = getservbyname( service, transport))
		sin.sin_port = pse->s_port;
	else if((sin.sin_port = htons((unsigned short)atoi(service))) == 0 )
		errexit("can't get \"%s\" service entry \n", service);
	if( phe = gethostbyname( host ) )
		memcpy( &sin.sin_addr, phe->h_addr, phe->h_length);
	else if( ( sin.sin_addr.s_addr = inet_addr(host) ) == INADDR_NONE)
		errexit("can't get \"%s\" host entry ", host);
	if(( ppe = getprotobyname( transport)) == 0 )
		errexit("can't get\"s\" protocol entry \n", transport);
		*/
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons( 4321 );
	if( strcmp( transport, "udp") == 0 )
		type = SOCK_DGRAM;
	else
		type = SOCK_STREAM;
//	s = socket(PF_INET, type, ppe->p_proto);
	s = socket(AF_INET, type, 0 );
	if( s < 0 )
		errexit("can't create socket: %s \n", strerror( errno ));
	if( connect( s, (strcut sockaddr*)&sin, sizeof(sin)) < 0)
		errexit("can't connect to %s.%s:%s\n", host, service, \
				strerror(errno));
}




















